package Welsy4.Connect.model
{
	import Welsy4.Connect.vo.*;
	import Welsy4.Connect.vo.HttpServiceHandler;
	
	import com.adobe.cairngorm.model.ModelLocator;    

	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	import mx.controls.Alert;
	import Welsy4.Connect.events.ResponseEvent;
	
    [Bindable]
    public class ConnectModel implements com.adobe.cairngorm.model.ModelLocator
    {
        private static var modelLocator : Welsy4.Connect.model.ConnectModel;
        
		public var WELSY_LOGIN_NICKNAME:String = "fminzoni";
		public var WELSY_LOGIN_PASSWORD:String = "pippone";
		
		/* Possible Connect Error Code Values */
		public static var SESSION_OK:String = "Session OK";
		public static var REQUIRE_LOGIN:String  = "Login Required";
		public static var INVALID_REQUEST:String = "Invalid Request Error";
		public static var NO_DATA_RESPONSE:String = "No data Error";
		public static var NO_ACCESS_RESPONSE:String = "No access Error";
		public static var TOO_MUCH_DATA_RESPONSE:String = "Too much data Error";
		public static var BAD_REQUEST:String = "Generic Request Error";
		
		/* Paossible connect error */
		public static var CONNECTION:int = -1;
		public static var INVALID:int = 0;
		public static var NO_DATA:int = 1;
		public static var DENIED:int = 2;
		public static var GENERIC:int = 3;
		
		
		public static var MAX_LOGIN_REQUEST:int = 10;
		public static var MAX_LOGIN_REQUEST_REACHED:String = "Error in connection. Contact the authorities";
		
		
		
        public static function getInstance() : Welsy4.Connect.model.ConnectModel 
        {
            if ( modelLocator == null )
                modelLocator = new Welsy4.Connect.model.ConnectModel();
                
            return modelLocator;
        }
       
        public function ConnectModel() 
        {
		    /* fake event listener */
			if ( Welsy4.Connect.model.ConnectModel.modelLocator != null )
                throw new Error( "Only one ModelLocator instance should be instantiated" );    
        }
	   
		/* fake event function */
		public function showLogout(event:ResponseEvent):void {
			Alert.show("Logged OUT "+ event.ResultCode + " " + event.Status.toString() + " " + User(event.ResultObject).nickname);
			}
		
	    public function addQueue(queue:String):void {
			if(DEBUG_MODE == true){
				this.queue.push([queue, User.getLoginRequestlocaleTime()]);
			}
	    }
		
		public function pauseQueue():void {
			this.queue.push([" - ", " - "]);
		}
		
	    public function readQueue():String {
			var queueString:String = "";
			for (var i:String in queue) {
				queueString += queue[i][0] + " : " + queue[i][1] + "\n";
				}
			return queueString
	    }
       
	    public var userLogin : LoginVO;
        public var isLogged : Boolean = false;
	    public var httpServiceHandler:HttpServiceHandler;
        
	    public var sessionCookie: String ="";
	    public var currentLoginRequest:int = 0;
	    
		public var currentUser:User;
		public var expectedUser:User;
		public var lastUserFound:User;
		public var lastUserSearched:User;
		public var lastMeetingCreated:Meeting;
		public var lastMeetingFound:Meeting;
		public var lastMeetingUpdated:Meeting;
		public var lastMeetingParticipantsAdded:MeetingParticipants;
		
		private var queue:Array = new Array();
		
		public var APIServicesEvents:Array = new Array(); // array degli eventi in APIServicesEvents
		
		public var myMeetings:Array = new Array(); // Array di istanze vo.Meeting
		
		public var SCOShortcutRequested :SCOShortcut; // SCO shortcut requested (see SCOShortcutCommand.result)
		
		public var DEBUG_MODE:Boolean = false;
        
    }    
}


